const path = require("path");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const CopyPlugin = require("copy-webpack-plugin");
module.exports = {
    mode:'production',
    entry: './src/index.js',
	devtool:'source-map',
    output: {
        path: path.resolve(__dirname,"dist"),
        filename: '[name].js',
		clean: true
    },
    module: {
		rules:[
			{
				test:/\.css$/,
		        use:[
					MiniCssExtractPlugin.loader,
					// 'style-loader',
					'css-loader',
					'postcss-loader'
				],
		        include:path.join(__dirname,'./src'),
		        exclude:/node_modules/
		    },
			{
			    test:/\.less$/,
				use:[
					MiniCssExtractPlugin.loader,
					// 'style-loader',
					'css-loader',
					'less-loader',
					'postcss-loader'
				],
				include:path.join(__dirname,'./src'),
				exclude:/node_modules/
			},
			{
				test:/\.scss$/,
				use: [
					MiniCssExtractPlugin.loader,
					// 'style-loader',
					'css-loader',
					'sass-loader',
					'postcss-loader'
				],
				include:path.join(__dirname,'./src'),
				exclude:/node_modules/
			},
			{
				test:/\.(jpg|png|gif)$/i,
				type:'asset',
				generator:{
					filename:'imgs/[hash][ext][query]'
				},
				parser:{
					dataUrlCondition:{
						maxSize: 8192
					}
				},
				include:path.join(__dirname,'./src'),
				exclude:/node_modules/
			},
			{
			    test: /(iconfont.svg)|\.(woff|woff2|eot|ttf|otf)$/,
		        type:'asset/resource',
			    generator:{
					filename:'fonts/[hash][ext][query]'
			    },
				include:path.join(__dirname,'./src'),
				exclude:/node_modules/
			}
		]
    },
    plugins: [
		new MiniCssExtractPlugin({
		    filename: 'css/[name].css'
		}),
		new HtmlWebpackPlugin({
			minify: {
				removeAttributeQuotes: true,
			},
			hash: true,
			template: './public/index.html',
			filename: 'index.html'
		}),
		new CopyPlugin({
		        patterns: [
		            {
		                from: "public/**/*.*", 
		                // to: "./",
		                // to: "[name][ext]",
						 to({ context, absoluteFilename }) {
		                    var targetPath = absoluteFilename.replace('public','dist');
		                    return targetPath;
		                },
		                filter: (resourcePath) => {
		                    if(resourcePath.indexOf('index.html')>-1){
		                        return false
		                    }
		                    return true;
		                }
		            }
		        ],
		    })
    ],
    devServer:{
		static:path.resolve(__dirname,'dist'),
		host:'localhost',
		compress:true,
		port:8080
    }
}
